package gorails.com;







import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class startup extends TabActivity implements OnGestureListener {
    /** Called when the activity is first created. */
	// Change
	private static float velocity = 0;
    private GestureDetector gestureScanner;
    private static int index = 0;
    private ViewFlipper flip;
    private String origin;
    private SQLiteDatabase db;
    static final int PROGRESS_DIALOG = 0;

    ProgressDialog progressDialog;

    //---Database--
    private static final String TABLE_LINE = "line";
    private static final String TABLE_STATION = "station";
    private static final String LINE_COLUMN = "metra";
    private static final String COLUMN_SIZE = "stationSize";
    	
    private static final String COLUMN_UPN = "UnionPacificNorth";
	private static final String COLUMN_MDN = "MilwaukeeDistrictNorth";
	private static final String COLUMN_NCS = "NorthCentralService";
	private static final String COLUMN_UPNW = "UnionPacificNorthwest";
	private static final String COLUMN_MDW = "MilwaukeeDistrictWest";
	private static final String COLUMN_UPW = "UnionPacificWest";
	private static final String COLUMN_BNSF = "BNSFRailway";
	private static final String COLUMN_HC = "HeritageCorridor";
	private static final String COLUMN_SWS = "SouthwestService";
	private static final String COLUMN_RID = "RockIslandDistrict";
	private static final String COLUMN_MED = "MetraElectricDistrict";
	
	private DBAdapter dbAdapter;
    //---end ---
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       
      
        
        
        
        
        
        
        
        
        
        
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab
        gestureScanner = new GestureDetector(this); // Set the GestureDector for this 
        flip=(ViewFlipper)findViewById(R.id.flip);

		
        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, TripPlannerActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("tripplanner").setIndicator("Trip Planner",
        		res.getDrawable(R.drawable.ic_tab_tripplanner))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        /*intent = new Intent().setClass(this, FareCalculatorActivity.class);
        spec = tabHost.newTabSpec("farecalculator").setIndicator("Fare Calc",
        		res.getDrawable(R.drawable.ic_tab_farecalculator))
                      .setContent(intent);
        tabHost.addTab(spec);*/
        
        intent = new Intent().setClass(this, MapActivity.class);
        spec = tabHost.newTabSpec("map").setIndicator("Map",
                          res.getDrawable(R.drawable.ic_tab_map))
                      .setContent(intent);
        tabHost.addTab(spec);

        /*intent = new Intent().setClass(this, AdvisoriesActivity.class);
        spec = tabHost.newTabSpec("advisories").setIndicator("Advisories",
                          res.getDrawable(R.drawable.ic_tab_advisories))
                      .setContent(intent);
        tabHost.addTab(spec);*/
        
        intent = new Intent().setClass(this, AdvisoriesActivity.class);
        spec = tabHost.newTabSpec("helfinfo").setIndicator("Help & Info",
                          res.getDrawable(R.drawable.ic_tab_helpinfo))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

        
        
    }
    @Override
    public void onResume(){
    	super.onResume();   	

    }
    
    protected Dialog onCreateDialog(int id) {
        switch(id) {
        case PROGRESS_DIALOG:
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage("Updating");
            progressDialog.setTitle("Update");
            return progressDialog;
        default:
            return null;
        }
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        switch(id) {
        case PROGRESS_DIALOG:
            progressDialog.setProgress(0);
            break;
        default: break;
        }
    }

    
    @Override
    public void onStart(){
    	super.onStart();   	
    	
        
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent me){
        return gestureScanner.onTouchEvent(me);
    }
    @Override 
    public boolean onDown(MotionEvent e){
    	//text1.setText("-" + "DOWN" + "-");
        return true; 
    }
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){    	
    	HorizontalScrollView hScroll = (HorizontalScrollView) findViewById(R.id.HScroll);
    	TabHost tabHost = getTabHost(); 
    	if(velocityX<-1000){
    		//go forward
    		if(index<2){
    			index++;
        		flip.setInAnimation(inFromRightAnimation());
            	flip.setOutAnimation(outToLeftAnimation());
            	flip.showNext();      
            	tabHost.setCurrentTab(index);
            	hScroll.scrollBy(100, 0);
    		}
    	}
    	else if(velocityX>1000){
    		//go back
    		if(index!=0){
    			index--;
    			
    			flip.setInAnimation(inFromLeftAnimation());
        		flip.setOutAnimation(outToRightAnimation());
        		flip.showPrevious();      

    			tabHost.setCurrentTab(index);
    			hScroll.scrollBy(-100, 0);
    		}
    	}    	
        return true; 
    }
    private Animation inFromRightAnimation() {
   	 
    	Animation inFromRight = new TranslateAnimation(
    	Animation.RELATIVE_TO_PARENT,  +1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
    	Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    	);
    	inFromRight.setDuration(500);
    	inFromRight.setInterpolator(new AccelerateInterpolator());
    	return inFromRight;
    }
    private Animation outToLeftAnimation() {
    	Animation outtoLeft = new TranslateAnimation(
    	Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  -1.0f,
    	Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    	);
    	outtoLeft.setDuration(500);
    	outtoLeft.setInterpolator(new AccelerateInterpolator());
    	return outtoLeft;
    }
    private Animation inFromLeftAnimation() {
    	Animation inFromLeft = new TranslateAnimation(
    		Animation.RELATIVE_TO_PARENT,  -1.0f, Animation.RELATIVE_TO_PARENT,  0.0f,
    		Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    	);
    	inFromLeft.setDuration(500);
    	inFromLeft.setInterpolator(new AccelerateInterpolator());
   		return inFromLeft;
    }
    private Animation outToRightAnimation() {
    	Animation outtoRight = new TranslateAnimation(
    			Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f,
    			Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
    	);
    	outtoRight.setDuration(500);
    	outtoRight.setInterpolator(new AccelerateInterpolator());
    	return outtoRight;
    }
    @Override 
    public void onLongPress(MotionEvent e){
    }
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){
    	
        return true;
    } 
    @Override 
    public void onShowPress(MotionEvent e){
    }    
    @Override 
    public boolean onSingleTapUp(MotionEvent e){
        return true;
    }
    
	
    
}